

package com.mt.restaurant.controller.material;

import org.springframework.security.access.prepost.PreAuthorize;
import com.mt.common.core.annotation.ApiPageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.mt.common.core.web.base.PageDTO;
import com.mt.common.core.web.base.PageResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.mt.restaurant.dto.material.MaterialOrderEditDto;
import com.mt.restaurant.entity.material.MaterialOrder;
import com.mt.restaurant.service.material.MaterialOrderService;


@Api(tags = "材料订单")
@RestController
@RequestMapping("/api/restaurant/material/MaterialOrder")
@CrossOrigin(allowCredentials = "true")
public class MaterialOrderController {
    private static Logger logger = LoggerFactory.getLogger(MaterialOrderController.class);


    @Autowired
    private MaterialOrderService materialOrderService;

    /**
     * 根据分页参数查询材料订单集合
     *
     * @param pageDTO 分页条件
     */
//    @PreAuthorize("hasAuthority('restaurant:material:MaterialOrder:view')")
    @ApiOperation("分页查询材料订单")
    @ApiPageParam
    @PostMapping("/findMaterialOrders")
    public PageResultDTO findMaterialOrders(@RequestBody PageDTO pageDTO) {
        return this.materialOrderService.findMaterialOrders(pageDTO);
    }

    /**
     * 根据ID查询指定的材料订单
     *
     * @param materialOrderId Id
     */
    @PreAuthorize("hasAuthority('restaurant:material:MaterialOrder:view')")
    @ApiOperation("根据id查询材料订单")
    @ApiPageParam
    @PostMapping("/findMaterialOrder")
    public MaterialOrder findMaterialOrder(@RequestParam Long materialOrderId) {
        return this.materialOrderService.findMaterialOrder(materialOrderId);
    }

    /**
     * 根据ID查询指定的材料订单(包含外键名称)
     *
     * @param materialOrderId Id
     */
    @PreAuthorize("hasAuthority('restaurant:material:MaterialOrder:view')")
    @ApiOperation("根据ID查询指定的材料订单(包含外键名称)")
    @PostMapping("/findMaterialOrderForView")
    public MaterialOrder findMaterialOrderForView(@RequestParam Long materialOrderId) {
        return this.materialOrderService.findMaterialOrderWithForeignName(materialOrderId);
    }

    /**
     * 根据ID查询指定的材料订单(包含材料订单和外键名称)
     *
     * @param materialOrderId Id
     */
    @PreAuthorize("hasAuthority('restaurant:material:MaterialOrder:view')")
    @ApiOperation("根据ID查询指定的材料订单(包含材料订单和外键名称)")
    @PostMapping("/findMaterialOrderForEdit")
    public MaterialOrderEditDto findMaterialOrderForEdit(@RequestParam Long materialOrderId) {
        MaterialOrderEditDto materialOrderEditDto = new MaterialOrderEditDto();
        materialOrderEditDto.setMaterialOrder(this.materialOrderService.findMaterialOrderWithForeignName(materialOrderId));

        this.prepareMaterialOrderEditDto(materialOrderEditDto);

        return materialOrderEditDto;
    }

    /**
     * 根据ID查询指定的材料订单(只提取ID 和 Name)
     *
     * @param materialOrderId Id
     */
    @PreAuthorize("hasAuthority('restaurant:material:MaterialOrder:view')")
    @ApiOperation("根据ID查询指定的材料订单(只提取ID 和 Name)")
    @PostMapping("/findMaterialOrdersWithIdNameById")
    public MaterialOrder findMaterialOrdersWithIdNameById(@RequestParam Long materialOrderId) {
        return this.materialOrderService.findMaterialOrdersWithIdNameById(materialOrderId);
    }

    /**
     * 根据名称查询材料订单集合(只提取ID 和 Name)
     *
     * @param materialOrderName 名称
     */
    @PreAuthorize("hasAuthority('restaurant:material:MaterialOrder:view')")
    @ApiOperation("根据名称查询材料订单集合(只提取ID 和 Name)")
    @PostMapping("/findMaterialOrdersWithIdNameByName")
    public List<MaterialOrder> findMaterialOrdersWithIdNameByName(@RequestParam String materialOrderName) {
        return this.materialOrderService.findMaterialOrdersWithIdNameByName(materialOrderName);
    }


    /**
     * 创建新的材料订单
     */
    @PreAuthorize("hasAuthority('restaurant:material:MaterialOrder:add')")
    @ApiOperation("创建新的材料订单")
    @PostMapping("/createMaterialOrder")
    public MaterialOrderEditDto createMaterialOrder() {
        MaterialOrderEditDto materialOrderEditDto = new MaterialOrderEditDto();
        materialOrderEditDto.setMaterialOrder(new MaterialOrder());

        this.prepareMaterialOrderEditDto(materialOrderEditDto);
        return materialOrderEditDto;
    }

    private void prepareMaterialOrderEditDto(MaterialOrderEditDto materialOrderEditDto) {

    }

    /**
     * 新增保存材料订单
     *
     * @param materialOrder 实体对象
     */
    @PreAuthorize("hasAuthority('restaurant:material:MaterialOrder:add')")
    @ApiOperation("新增保存材料订单")
    @PostMapping("/saveMaterialOrder")
    public MaterialOrder saveMaterialOrder(@RequestBody MaterialOrder materialOrder) {
        return this.materialOrderService.saveMaterialOrder(materialOrder);
    }

    /**
     * 修改保存材料订单
     *
     * @param materialOrder 实体对象
     */
    @PreAuthorize("hasAuthority('restaurant:material:MaterialOrder:update')")
    @ApiOperation("修改保存材料订单")
    @PostMapping("/updateMaterialOrder")
    public MaterialOrder updateMaterialOrder(@RequestBody MaterialOrder materialOrder) {
        return this.materialOrderService.updateMaterialOrder(materialOrder);
    }


    /**
     * 根据ID删除材料订单
     *
     * @param materialOrderId ID
     */
    @PreAuthorize("hasAuthority('restaurant:material:MaterialOrder:remove')")
    @ApiOperation("根据ID删除材料订单")
    @PostMapping("/deleteMaterialOrder")
    public void deleteMaterialOrder(@RequestParam Long materialOrderId) {
        this.materialOrderService.deleteMaterialOrder(materialOrderId);
    }

    /**
     * 更新材料订单状态
     *
     * @param materialOrderId
     * @param auditOperate
     */
    @PreAuthorize("hasAuthority('restaurant:material:MaterialOrder:update')")
    @ApiOperation("根据ID提交审核")
    @PostMapping("/updateMaterialOrdeStatus")
    public void updateMaterialOrdeStatus(@RequestParam Long materialOrderId, @RequestParam Integer auditOperate) {
        this.materialOrderService.updateMaterialOrdeStatus(materialOrderId, auditOperate);
    }


}

